(function(_scope) {

var DU_TAG = '0.0.7';
//var DU_TAG = 0;

if (_scope['__'] == undefined) {
  _scope['__'] = {};
}
var __ = _scope['__'];

if (__.config == undefined) {
  __.config = {}
}

__.config.loaderBase = [
  '/misc', //production
  'http://double-underscore.googlecode.com/svn/tags', //google code base
  'http://double-underscore.googlecode.com/svn/yui/2.5.2/build',
  'http://localhost/stauren.net/develop/3party/yui252/build',
  'http://double-underscore.googlecode.com/svn/trunk/src' //google code trunk base
];
__.config.loaderModules = {};

if (!__.load) {
  var _SC = __.config,
  _setNewvalue = function(varName,newValue){ //set value to a member, and create the object link
    var bAry=varName.split(/\./);
    var d=window, key;
    for(var i=0,j=bAry.length-1;i<j;i++){
      d[bAry[i]] = d[bAry[i]] || {};
      d=d[bAry[i]]
    }
    key = bAry[bAry.length-1];
    return d[key] = newValue || (d[key] === undefined ? {} : d[key])
  }, _getValue = function(varName,newValue){ //get value, and create the object link
    var bAry=varName.split(/\./);
    var d=window;
    for(var i=0,j=bAry.length;i<j;i++){
      d[bAry[i]] = d[bAry[i]] || {};
      d=d[bAry[i]]
    }
    return d
  }, _isSet = function (varName){ //check if the the object link exist
    var bAry=varName.split(/\./),
      d=window;
    for(var i=0,j=bAry.length;i<j;i++){
      if (d[bAry[i]] == undefined) {
        return false;
      }
      d = d[bAry[i]];
    }
    return true;
  },
  /**
   * call a function if some requirements are satisfied
   * usually used for lazy load and initiate
   * fnCall: the init function, 
   * fnJudge: mixed, could be a function or a string(a set global var)
   * iInterval: milliseconds of interval
   */
  _callFunction = function(fnCall, judge, iInterval) {
    iInterval = iInterval || 1000;
    var _fnJudge = typeof judge=='function' ? judge : function() {
      var _bRet = false;
      if(_isSet(judge)) {
        _bRet = true;
      }
      return _bRet;
    }, _sId;

    // do it now
    if (_fnJudge()) {
      fnCall();
    } else {
    //or later
      _sId = setInterval(function() {
        if(_fnJudge()) {
          clearInterval(_sId);
          fnCall();
        }
      }, iInterval);
    }
  },
  _loadFile = function(type,url,fn) {
    var head = document.getElementsByTagName("head")[0];
   /* if (false && !inHead) {
      //loadFile while page loading
      if(type=="script"){
        document.write('<scr'+'ipt src="'+url+'" type="text/javascript"><\/scr'+'ipt>')
      } else if(type=="css"){
        document.write('<link href="'+url+'" type="text/css" rel="stylesheet"></link>')
      }
    } else {*/
      //loadFile after page is loaded, lots of loading may crash IE
      var node, usera = __.config.ua;
      fn=typeof fn == 'function' ? fn : function(){};
      if(type=="script"){
        node = document.createElement('script');
        node.type = 'text/javascript';
        node.src = url;
        node.charset = 'utf-8';
      } else if(type=="css") {
        node = document.createElement('link');
        node.type = 'text/css';
        node.rel = 'stylesheet';
        node.href = url;
        node.charset = 'utf-8';
      }
      if (usera.IE) {
        node.onreadystatechange = function() {
          var rs = this.readyState;
          if ("loaded" === rs || "complete" === rs) {
            fn();
          }
        };
      } else if (usera.webkit) {
        if (type === "script") {
          node.addEventListener("load", function() {
            fn();
          });
        } 
      } else {
        node.onload = function() {
          fn();
        }
      }
      head.appendChild(node);
   // }
  }, _loadModuleByName = function(modName,getObj) {
    var loadingItem = _SC.loaderModules[":"+modName];
   /* if (getObj && getObj.dynamic) {
      inHead = !!getObj.dynamic;
    }*/
    if(!loadingItem){
      _loadFile('script', modName, getObj && getObj.onSuccess)
    } else {
      var type = (loadingItem.detailObj && loadingItem.detailObj.type) || 'script';
      _loadFile(type,loadingItem.get(getObj), getObj && getObj.onSuccess)
    }
  }, _load = function(n,getObj) {
    if (typeof n=='object' && n.length > 0) {
      for (var i=0,j=n.length;i<j;i++) {
        _loadModuleByName(n[i], getObj);
      }
    } else {
      n = n.split(' ');
      var newObj, firstN = n.shift();
      if (n.length >= 1)  {
        newObj = { 
          onSuccess : function() { _load(n.join(' '), getObj); }
        }
      } else {
        newObj = getObj;
      }

      _loadModuleByName(firstN, newObj);
    }
  }, _LoadModule = function (modName, detailObj) {
    this.mname = modName;
    this.detailObj = detailObj || {};
  }; 
  _LoadModule.prototype.get = function(getObj) {
    getObj = getObj || {};

    //default : local file
    var isDebug = _SC.debug === true ? true : false,
      type = this.detailObj['type'] || 'js',
      source = this.detailObj['source'] || 'DU',
      sBase, name = this.mname;

    switch (source) {
      case 'DU' :
        //product on double-undersocre, debug on local
        if (isDebug) {
          sBase = [_SC.loaderBase[0], type].join('/');
          name += '_comment';
        } else {
          var edition = getObj.version ? getObj.version : DU_TAG;
          if (edition == 0) { // load trunk
            sBase = _SC.loaderBase[4];
          } else { // load special tags
            sBase = [_SC.loaderBase[1], edition].join('/');
          }
        }
        break;
      case 'YUI' :
        //product on double-undersocre, debug on local
        if (isDebug) {
          sBase = [_SC.loaderBase[3], this.detailObj['module'] ? this.detailObj['module'] : name].join('/');
          name += this.detailObj['beta'] ? '-beta' : '';
        } else {
          sBase = [_SC.loaderBase[2], name].join('/');
          name += (this.detailObj['beta'] ? '-beta' : '') + '-min';
        }
        break;
      case 'ST' :
        //product on stauren.net, debug on local
          sBase = [_SC.loaderBase[0], type].join('/');
        if (isDebug) {
          name += '_comment';
        }
        break;
    }

    name += type == 'css' ? '.css' : '.js';
    if (isDebug) {
      name += '?_t='+Math.random();
    }
    return [sBase, name].join('/')
  };

  var ua = navigator.userAgent, cua = _setNewvalue('__.config.ua');
  if ((/KHTML/).test(ua)) {
    cua.webkit = true;
  } else if (/Opera/.test(ua)) {
    cua.opera = true;
  } else if (/MSIE/.test(ua)) {
    cua.IE = true;
  } else if (/Gecko/.test(ua)) {
    cua.Gecko = true;
  }


    var loadingList = _SC.loaderModules;
    loadingList[":bcss"]=new _LoadModule("base", {type:'css', source:'ST'});
    loadingList[":stcss"]=new _LoadModule("stauren", {type:'css', source:'ST'});
    loadingList[":b-stcss"]=new _LoadModule("base-stauren", {type:'css', source:'ST'});

    loadingList[":yeditorcss"]=new _LoadModule("yeditor", {type:'css', source:'ST'});
    loadingList[":yeditor"]=new _LoadModule("yeditor",{source:'ST'});

    loadingList[":stauren"]=new _LoadModule("du_core", {source:'ST'});
    loadingList[":validator"]=new _LoadModule("validator", {source:'ST'});
    loadingList[":ydescjac"]=new _LoadModule("ydescjac", {source:'ST'});

    loadingList[":yutil"]=new _LoadModule("utilities", {source:'YUI'});
    loadingList[":yselector"]=new _LoadModule("selector", {source:'YUI', beta:true});
    loadingList[":yjson"]=new _LoadModule("json", {source:'YUI'});
    loadingList[":yelement"]=new _LoadModule("element", {source:'YUI', beta:'true'});
    loadingList[":ymenu"]=new _LoadModule("menu", {source:'YUI'});
    loadingList[":ycontainercore"]=new _LoadModule("container_core", {source:'YUI',module:'container'});

    loadingList[":du"]=new _LoadModule("du_4load", {source:'DU'});
    loadingList[":stbase"]=new _LoadModule("staurennet", {source:'DU'});
    loadingList[":es"]=new _LoadModule("es", {source:'DU'});
    loadingList[":imagecropper"]=new _LoadModule("image_cropper", {source:'DU'});
    loadingList[":yui252"]=new _LoadModule("yui252", {source:'DU'});

    __.load = _load;
    __.set = _setNewvalue;
    __.get = _getValue;
    __.isSet = _isSet;
    __.toCall = _callFunction;
}

__.isSet('ST') || __.set('ST', __);

})(window);
